package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.AppealTypeBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 服务诉求查询
 */
public class AppealSearchActivity extends BaseActivity implements IPublicBeanView {

    @BindView(R.id.et_AppealName)
    EditText et_AppealName;
    @BindView(R.id.et_Location)
    EditText et_Location;
    @BindView(R.id.tv_AppealType)
    TextView tv_AppealType;
    @BindView(R.id.sp_AppealSource)
    Spinner sp_AppealSource;
    @BindView(R.id.tv_StartTime)
    TextView tv_StartTime;
    @BindView(R.id.tv_EndTime)
    TextView tv_EndTime;
    private String eventTypeID="";
    //述求来源
    private List<GlobalBean> eventSourceList;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_appeal_search);
        setTitleName("诉求查询");
        setTitleRight(R.string.search);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        PublicListPest  publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("诉求来源");//事件规模
    }


    @OnClick({R.id.tv_right,R.id.tv_StartTime,R.id.tv_EndTime,R.id.tv_AppealType})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
            case R.id.tv_AppealType:
                startActivityForResult(new Intent(this,AppealTypeActivity.class),5553);
                break;
            case R.id.tv_StartTime:
                BaseUtil.createDataPop(this,tv_StartTime);
                break;
            case R.id.tv_EndTime:
                BaseUtil.createDataPop(this,tv_EndTime);
                break;
        }
    }



    /**
     * 提交
     */
    private void submit() {
        String name=et_AppealName.getText().toString().trim();
        String location=et_Location.getText().toString().trim();
        String source=((GlobalBean)sp_AppealSource.getSelectedItem()).getId();
        String startTime=tv_StartTime.getText().toString().trim();
        String endTime=tv_EndTime.getText().toString().trim();
        Intent intent=new Intent();
        intent.putExtra("EventName",name);
        intent.putExtra("Location",location);
        intent.putExtra("EventSource",source);
        intent.putExtra("EventType",eventTypeID);
        intent.putExtra("StartTime",startTime);
        intent.putExtra("EndTime",endTime);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 5553://选择事件类别
                    AppealTypeBean.RowsBean typesBean= (AppealTypeBean.RowsBean) data.getSerializableExtra("AppealTypeBeanID");
                    tv_AppealType.setText(data.getStringExtra("PrentName")+"-"+typesBean.getDictName());
                    eventTypeID=typesBean.getDictId();
                    break;
            }
        }
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("诉求来源".equals(publicBean.getDomainName())){
            eventSourceList=publicBean.getDicts();
            if (eventSourceList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,eventSourceList);
                sp_AppealSource.setAdapter(adapter);
            }
        }
    }
}
